home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1997 April / EnigmA AMIGA RUN 17 (1997)(G.R. Edizioni)(IT)[!][issue 1997-04][EAR-CD].iso / EARCD / comm / bbs / Hydra11s.lha / HBBS / Source / Doors_System / XPR / XPRD_Source / initio.c < prev    next >
C/C++ Source or Header  |  1996-06-25  |  9KB  |  402 lines

  1. /*
  2.  
  3.     IO.c
  4.     ----
  5.     Definitions for XPR_IO-functions
  6.  
  7.     (C) 1990 Oliver Wagner, All Rights Reserved
  8.  
  9. */
  10.  
  11. #include <proto/exec.h>
  12. #include <proto/dos.h>
  13. #include <devices/serial.h>
  14. #include <devices/timer.h>
  15. #include <string.h>
  16.  
  17. #define print(s) Write(Output(),s,strlen(s))
  18.  
  19. #include "xproto.h"
  20.  
  21. void plog(char,char*,...);
  22.  
  23. /* external Intuition functions */
  24. extern int xpr_opendisplay(void);
  25. extern int xpr_closedisplay(void);
  26. extern long xpr_displayupdate(struct XPR_IO*);
  27. extern long xpr_chk4abort(void);
  28. extern long xpr_windowsig;
  29.  
  30. /* Global IO-Request for XPR */
  31. extern struct IOExtSer *xpr_serio;
  32.  
  33. /* external filename array */
  34. extern char *xprfilearray[];
  35.  
  36. /* static Data */
  37. static struct timerequest treq;
  38. static struct MsgPort *tport;
  39. static char timeropen;
  40.  
  41. extern short nowindow;
  42.  
  43. /* The timer functions */
  44. static long opentimer(void)
  45. {
  46.     tport=CreatePort(0,0);
  47.     if(!tport) return(-1);
  48.     treq.tr_node.io_Message.mn_ReplyPort=tport;
  49.     if(OpenDevice("timer.device",UNIT_VBLANK,(struct IORequest *)&treq,0)) return(-1);
  50.     timeropen=1;
  51.     return(0);
  52. }
  53. static void closetimer(void)
  54. {
  55.     if(timeropen) CloseDevice((struct IORequest *)&treq);
  56.     if(tport) DeletePort(tport);
  57. }
  58. static void qtimer(long micros)
  59. {
  60.     int secs=0;
  61.     if (micros > 1000000) {
  62.    secs = micros / 1000000;
  63.    micros = micros % 1000000;
  64.     }
  65.     treq.tr_time.tv_micro=micros;
  66.     treq.tr_time.tv_secs=secs;
  67.     treq.tr_node.io_Command=TR_ADDREQUEST;
  68. /*    printf("QTIMER s=%ld m=%ld\n",secs,micros);*/
  69.     SendIO((struct IORequest *)&treq);
  70. }
  71.  
  72. /* the finfo-function */
  73. static long __saveds xpr_finfo(char *name,long type)
  74. {
  75.     struct FileInfoBlock *fib=AllocMem(sizeof(struct FileInfoBlock),0);
  76.     BPTR lck;
  77.     long result=0;
  78.  
  79.     plog('+',"FINFO: %s (%ld)\n",name,type);
  80.     if(!fib) return(0);
  81.     if(!(lck=Lock(name,SHARED_LOCK))) goto xit;
  82.     Examine(lck,fib);
  83.     UnLock(lck);
  84.     result=fib->fib_Size;
  85.     if(type==2) result=1;
  86.     else if(type!=1) result=0;
  87. xit:
  88.     FreeMem(fib,sizeof(struct FileInfoBlock));
  89. /*    printf("result=%ld\n",result);*/
  90.     return(result);
  91. }
  92.  
  93. // *G*
  94.  
  95. /* the serwrite function */
  96. static long __saveds xpr_swrite(char *buffer,long size)
  97. {
  98. /*    printf("SWRITE: buffer=%lx size=%ld\n",buffer,size);*/
  99.     xpr_serio->IOSer.io_Length=size;
  100.     xpr_serio->IOSer.io_Data=buffer;
  101.     xpr_serio->IOSer.io_Command=CMD_WRITE;
  102.     DoIO((struct IORequest *)xpr_serio);
  103.     return((long)(struct IORequest *)xpr_serio->IOSer.io_Error);
  104. }
  105.  
  106. // *G*
  107.  
  108. long chk4cd(void)
  109. {
  110.   xpr_serio->IOSer.io_Command=SDCMD_QUERY;
  111.   DoIO((struct IORequest *)xpr_serio);
  112.   return(!(xpr_serio->io_Status&(1<<5)));
  113. }
  114.  
  115. // *G*
  116.  
  117. /* the serread function */
  118. static long __saveds xpr_sread(char *buffer,long size,long timeout)
  119. {
  120.   long flag=xpr_windowsig|(1<<xpr_serio->IOSer.io_Message.mn_ReplyPort->mp_SigBit);
  121.   long len,nflag;
  122.  
  123.   /*printf("SREAD() buffer=%lx size=%ld timeout=%ld\n",buffer,size,timeout);*/
  124.   SetSignal(0,flag);
  125.   if(timeout)
  126.   {
  127.     flag|=1<<tport->mp_SigBit;
  128.     /*printf("Starting timer...\n");*/
  129.     qtimer(timeout);
  130.   }
  131.   else
  132.   {
  133.     xpr_serio->IOSer.io_Command=SDCMD_QUERY;
  134.     DoIO((struct IORequest *)xpr_serio);
  135.     if(!(len=xpr_serio->IOSer.io_Actual))
  136.     {
  137.       return(0);
  138.     }
  139.     else
  140.     {
  141.       if(len>size) len=size;
  142.       xpr_serio->IOSer.io_Command=CMD_READ;
  143.       xpr_serio->IOSer.io_Data=buffer;
  144.       xpr_serio->IOSer.io_Length=len;
  145.       DoIO((struct IORequest *)xpr_serio);
  146.       /*printf("Fastread return %ld\n",xpr_serio->IOSer.io_Actual);*/
  147.       return((long)xpr_serio->IOSer.io_Actual);
  148.     }
  149.   }
  150.   SetSignal(0,flag);
  151.   xpr_serio->IOSer.io_Command=CMD_READ;
  152.   xpr_serio->IOSer.io_Data=buffer;
  153.   xpr_serio->IOSer.io_Length=size;
  154.   SendIO((struct IORequest *)xpr_serio);
  155.   nflag=Wait(flag);
  156.   if(nflag&xpr_windowsig)
  157.   {
  158.     AbortIO((struct IORequest *)&treq);
  159.     AbortIO((struct IORequest *)xpr_serio);
  160.     WaitIO((struct IORequest *)&treq);
  161.     WaitIO((struct IORequest *)xpr_serio);
  162.     return(-1);
  163.   }
  164.   if(nflag&(1<<xpr_serio->IOSer.io_Message.mn_ReplyPort->mp_SigBit))
  165.   {
  166.     AbortIO((struct IORequest *)&treq);
  167.     WaitIO((struct IORequest *)xpr_serio);
  168.     WaitIO((struct IORequest *)&treq);
  169.     /*printf("Normal read returns %ld (error %ld)\n",xpr_serio->IOSer.io_Actual,(long)(struct IORequest *)xpr_serio->IOSer.io_Error);*/
  170.     return((long)xpr_serio->IOSer.io_Actual);
  171.   }
  172.   /*printf("TIMEOUT:\n");*/
  173.   AbortIO((struct IORequest *)xpr_serio);
  174.   WaitIO((struct IORequest *)&treq);
  175.   WaitIO((struct IORequest *)xpr_serio);
  176.   return((long)xpr_serio->IOSer.io_Actual);
  177. }
  178.  
  179. // *G*
  180.  
  181. /* sflush - flushes serial port */
  182. static long __saveds xpr_sflush(void)
  183. {
  184.     xpr_serio->IOSer.io_Command=CMD_FLUSH;
  185.     DoIO((struct IORequest *)xpr_serio);
  186.     return((long)xpr_serio->IOSer.io_Error);
  187. }
  188.  
  189. // *G*
  190.  
  191. /* setserial; stolen from TERM by Olsen */
  192. #define ST_PARTYON  (1 << 0)
  193. #define ST_PARTYODD (1 << 1)
  194. #define ST_7WIRE  (1 << 2)
  195. #define ST_QBREAK (1 << 3)
  196. #define ST_RADBOOGIE  (1 << 4)
  197. #define ST_SHARED (1 << 5)
  198. #define ST_EOFMODE  (1 << 6)
  199. #define ST_XDISABLED  (1 << 7)
  200. #define ST_PARTYMARKON  (1 << 8)
  201. #define ST_PARTYMARK  (1 << 9)
  202. #define ST_2BITS  (1 << 10)
  203. #define ST_READ7  (1 << 11)
  204. #define ST_WRITE7 (1 << 12)
  205. static long __saveds xpr_setserial(long Status)
  206. {
  207.   STATIC LONG XprBauds[12] =
  208.   {
  209.     110,
  210.     300,
  211.     1200,
  212.     2400,
  213.     4800,
  214.     9600,
  215.     19200,
  216.     31250,
  217.     38400,
  218.     57600,
  219.     76800,
  220.     115200
  221.   };
  222.  
  223.   LONG Return,i;
  224.  
  225.   xpr_serio -> IOSer . io_Command = SDCMD_QUERY;
  226.   DoIO((struct IORequest *)xpr_serio);
  227.  
  228.   Return = xpr_serio -> io_SerFlags & 0xFF;
  229.  
  230.   if(xpr_serio -> io_ExtFlags & SEXTF_MSPON)
  231.     Return |= ST_PARTYMARKON;
  232.  
  233.   if(xpr_serio -> io_ExtFlags & SEXTF_MARK)
  234.     Return |= ST_PARTYMARK;
  235.  
  236.   if(xpr_serio -> io_StopBits == 2)
  237.     Return |= ST_2BITS;
  238.  
  239.   if(xpr_serio -> io_ReadLen == 7)
  240.     Return |= ST_READ7;
  241.  
  242.   if(xpr_serio -> io_WriteLen == 7)
  243.     Return |= ST_WRITE7;
  244.  
  245.   for(i = 0 ; i < 12 ; i++)
  246.   {
  247.     if(xpr_serio -> io_Baud == XprBauds[i])
  248.     {
  249.       Return |= (i << 16);
  250.  
  251.       break;
  252.     }
  253.   }
  254.  
  255.   if(Status != -1)
  256.   {
  257.     xpr_serio -> IOSer . io_Command = SDCMD_SETPARAMS;
  258.  
  259.     xpr_serio -> io_SerFlags    = Status & 0xFF;
  260.     xpr_serio -> io_ExtFlags    = 0;
  261.  
  262.     if(Status & ST_PARTYMARKON)
  263.       xpr_serio -> io_ExtFlags |= SEXTF_MSPON;
  264.  
  265.     if(Status & ST_PARTYMARK)
  266.       xpr_serio -> io_ExtFlags |= SEXTF_MARK;
  267.  
  268.     if(Status & ST_2BITS)
  269.       xpr_serio -> io_StopBits = 2;
  270.     else
  271.       xpr_serio -> io_StopBits = 1;
  272.  
  273.     if(Status & ST_READ7)
  274.       xpr_serio -> io_ReadLen = 7;
  275.     else
  276.       xpr_serio -> io_ReadLen = 8;
  277.  
  278.     if(Status & ST_WRITE7)
  279.       xpr_serio -> io_WriteLen = 7;
  280.     else
  281.       xpr_serio -> io_WriteLen = 8;
  282.  
  283.     DoIO((struct IORequest *)xpr_serio);
  284.  
  285.   }
  286.  
  287.   return(Return);
  288.  
  289. }
  290. /* EOT [end of theft] */
  291.  
  292.  
  293.  
  294. /* The ffirst() and ffnext() functions */
  295. static long __saveds xpr_ffirst(char *buffer,char *pattern)
  296. {
  297.     strcpy(buffer,xprfilearray[0]);
  298.     plog('+',"FFIRST: returning %s\n",buffer);
  299.     return(1);
  300. }
  301. static long __saveds xpr_fnext(long oc,char *buffer,char *pattern)
  302. {
  303.     if(!xprfilearray[oc]) {
  304.   plog('+',"FNEXT: end of array\n");
  305.   return(0);
  306.     }
  307.     else {
  308.   strcpy(buffer,xprfilearray[oc++]);
  309.   plog('+',"FNEXT: returning %s\n",buffer);
  310.   return(oc);
  311.     }
  312. }
  313.  
  314. static long __saveds xpr_gets(char*b,char*p)
  315. {
  316. /*    print("GETS: ");print(b);print(" - ");print(p);print("\n");*/
  317.     return(0);
  318. }
  319.  
  320. /* the stub routines for a4 loading :-( */
  321. static long __saveds xpr_fopen(char*a1,char*a2)
  322. {
  323.     long h;
  324.  
  325.     plog('+',"FOPEN: %s,'%s'\n",a1,a2);
  326.     switch(*a2) {
  327.   case 'r': return(Open(a1,MODE_OLDFILE));
  328.   case 'w': return(Open(a1,MODE_NEWFILE));
  329.   case 'a': if(h=Open(a1,MODE_OLDFILE)) {
  330.         Seek(h,0,OFFSET_END);
  331.         return(h);
  332.       }
  333.       return(Open(a1,MODE_NEWFILE));
  334.     }
  335.     return(0);
  336. }
  337. static long __saveds xpr_fclose(long *a1)
  338. {
  339.     if(a1) Close((BPTR)a1);
  340.     return(0);
  341. }
  342. static long __saveds xpr_fread(char*a1,long a2,long a3,long*fpt)
  343. {
  344. /*    printf("fread(%lx,%ld,%ld,%lx)\n",a1,a2,a3,fpt);*/
  345.     if(!a2) return(0);
  346.     return(Read((BPTR)fpt,a1,a2*a3));
  347. }
  348. static long __saveds xpr_fwrite(char*a1,long a2,long a3,long*fpt)
  349. {
  350. /*    printf("fwrite(%lx,%ld,%ld,%lx)\n",a1,a2,a3,fpt);*/
  351.     if(!a2) return(0);
  352.     return(Write((BPTR)fpt,a1,a2*a3));
  353. }
  354. static long __saveds xpr_fseek(long*a1,long a2,long a3)
  355. {
  356.     long h=-1;
  357. /*    printf("fseek(%lx,%ld,%ld)\n",a1,a2,a3);*/
  358.  
  359.     switch(a3) {
  360.   case 0: h=OFFSET_BEGINNING; break;
  361.   case 1: h=OFFSET_CURRENT; break;
  362.   case 2: h=OFFSET_END; break;
  363.   default: return(-1);
  364.     }
  365.     return((Seek((BPTR)a1,a2,h)!=-1)?0:-1);
  366. }
  367. static long __saveds xpr_unlink(char*a1)
  368. {
  369.     plog('+',"UNLINK: %s\n",a1);
  370.     return(DeleteFile(a1));
  371. }
  372.  
  373. /* init the xprIO and build everything */
  374. long init_xpr(struct XPR_IO *IO)
  375. {
  376.    IO->xpr_fopen     = xpr_fopen;
  377.    IO->xpr_fclose    = xpr_fclose;
  378.    IO->xpr_fread     = xpr_fread;
  379.    IO->xpr_fwrite    = xpr_fwrite;
  380.    IO->xpr_sread     = xpr_sread;
  381.    IO->xpr_swrite    = xpr_swrite;
  382.    IO->xpr_sflush    = xpr_sflush;
  383.    IO->xpr_update    = xpr_displayupdate;
  384.    IO->xpr_chkabort  = xpr_chk4abort;
  385.    IO->xpr_setserial = xpr_setserial;
  386.    IO->xpr_ffirst    = xpr_ffirst;
  387.    IO->xpr_fnext     = xpr_fnext;
  388.    IO->xpr_finfo     = xpr_finfo;
  389.    IO->xpr_fseek     = xpr_fseek;
  390.    IO->xpr_gets      = xpr_gets;
  391.    IO->xpr_unlink    = xpr_unlink;
  392. /*   xpr_sflush();*/
  393.    if(xpr_opendisplay()) return(-1);
  394.    return(opentimer());
  395. }
  396.  
  397. void close_xpr(void)
  398. {
  399.     xpr_closedisplay();
  400.     closetimer();
  401. }
  402.